package com.wds.stock.stockana;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.wds.stock.stockana.beans.StockInfoBean;
import com.wds.stock.stockana.events.GetAllStocksEvent;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;

public class FragmentPage1 extends Fragment {

	private View mFragmentRootView;
    private ListView mStockListLV;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(!EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().register(this);
    }

    @Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mFragmentRootView = inflater.inflate(R.layout.fragment_1, null);
        mStockListLV = (ListView) mFragmentRootView.findViewById(R.id.stockListView);
        StockInfoBean sb = new StockInfoBean();
        sb.setStName("中国中车");
        sb.setStNum("601111");
        sb.setStPrice(1.45f);
        sb.setStTrend(4.33f);
        List<StockInfoBean> sbList = new ArrayList<>();
        sbList.add(sb);
        sbList.add(sb);
        sbList.add(sb);
        sbList.add(sb);
        sbList.add(sb);
        sbList.add(sb);
        sbList.add(sb);
        sbList.add(sb);
        sbList.add(sb);
        sbList.add(sb);
        sbList.add(sb);
        mStockListLV.setAdapter(new StockListAdapter(getContext(),sbList));
        //LibCenter.getLibCenter(getContext()).doNetWorkReq(new GetAllStocks());
		return mFragmentRootView;
	}

    @Subscribe
    public void onEventMainThread(GetAllStocksEvent event){
        System.out.println("!!!!!!!!!");
        System.out.println(event.getAllStocks());

        //mStockListLV.setAdapter(new StockListAdapter(getContext(), event.getAllStocks()));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    class StockListAdapter extends BaseAdapter {

        List<StockInfoBean> stockList;
        Context context;

        class StocksHolder{
            public TextView stockNumTv;
            public TextView stockNameTv;
            public TextView stockPriceTv;
            public TextView trendTv;
        }
        public StockListAdapter(Context context,List<StockInfoBean> stockList) {
            super();
            this.context = context;
            this.stockList = stockList;
        }

        @Override
        public int getCount() {
            return stockList.size();
        }

        @Override
        public Object getItem(int position) {
            return stockList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            StocksHolder stocksHolder;
            if(null == convertView){
                convertView = LayoutInflater.from(context).inflate(R.layout.stocklist_item,null);
                stocksHolder = new StocksHolder();
                stocksHolder.stockNumTv = (TextView) convertView.findViewById(R.id.stockNumTv);
                stocksHolder.stockNameTv = (TextView) convertView.findViewById(R.id.stockNameTv);
                stocksHolder.stockPriceTv = (TextView) convertView.findViewById(R.id.stockPriceTv);
                stocksHolder.trendTv = (TextView) convertView.findViewById(R.id.trendTv);
                convertView.setTag(stocksHolder);
            }else {
                stocksHolder = (StocksHolder) convertView.getTag();
            }
            stocksHolder.stockNumTv.setText(stockList.get(position).getStNum());
            stocksHolder.stockNameTv.setText(stockList.get(position).getStName());
            stocksHolder.stockPriceTv.setText("￥" + stockList.get(position).getStPrice()+"");
            stocksHolder.trendTv.setText("+" + stockList.get(position).getStTrend()+"");
            return convertView;
        }
    }
}
